package com.cqb.cn.controller;

import com.cqb.cn.modle.User;

import com.cqb.cn.modle.bo.ErrorsModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * springMvc各种请求参数接收总结  https://www.cnblogs.com/eternityz/p/12442406.html?
 */
@RestController
@RequestMapping("recive")
@Slf4j
public class RequestParamesReciveController {
    //1.URL传参及注解@RequestParam
    @GetMapping(path = "/get1")
    public void get1(@RequestParam(name = "name") String name,
                     @RequestParam(name = "age") Integer age) {
        log.info("name:{},age:{}", name, age);
    }

    @GetMapping(path = "/get2")
    public void get2(User vo) {
        log.info("name:{},age:{}", vo.getName(), vo.getAge());
    }

    @GetMapping(path = "/get3")
    public void get3(HttpServletRequest request) {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        log.info("name:{},age:{}", name, age);
    }

    //    2.表单参数及注解@RequestParam
    @PostMapping(path = "/list")
    public void list(@RequestParam(name = "list") List<String> list) {
        log.info(String.valueOf(list));
    }

    @PostMapping(path = "/user")
    public void saveUsers(@RequestParam(name = "users") List<User> users) {
        log.info(String.valueOf(users));
    }

    //    3.JSON参数及注解@RequestBody
    @PostMapping(value = "/user-2")
    public User saveUser2(@RequestBody User user) {
        log.info(user.toString());
        return user;
    }

    //    4.URL路径参数及注解@PathVariable
    @GetMapping(value = "/user/{name}/{age}")
    public String findUser1(@PathVariable(value = "age") Integer age,
                            @PathVariable(value = "name") String name) {
        String content = String.format("name = %s,age = %d", name, age);
        log.info(content);
        return content;
    }

    /**
     * ①.带条件的 URL 参数 只有 /sex/F 或者 /sex/M 的请求才会进入 findUser2() 控制器方法，其他该路径前缀的请求都是非法的，会返回404状态码。
     * @param sex
     * @return
     */
    @GetMapping(value = "/sex/{sex:M|F}")
    public String findUser2(@PathVariable(value = "sex") String sex){
        log.info(sex);
        return sex;
    }

    /**
     * /call/doge;gender=male;group=programmer
     * @return
     */
//    @PostMapping(value = "/call/{name}")
//    public String find(@PathVariable(value = "name") String name,
//                       @MatrixVariable(value = "gender") String gender,
//                       @MatrixVariable(value = "group") String group) {
//        String content = String.format("name = %s,gender = %s,group = %s", name, gender, group);
//        log.info(content);
//        return content;
//    }
    //5.文件上传及MultipartFile
    @PostMapping(value = "/file1")
    public String file1(@RequestPart(name = "file1") MultipartFile multipartFile) {
        String content = String.format("name = %s,originName = %s,size = %d",
                multipartFile.getName(), multipartFile.getOriginalFilename(), multipartFile.getSize());
        log.info(content);
        return content;
    }
    @PostMapping(value = "/file2")
    public String file2(MultipartFile file1) {
        String content = String.format("name = %s,originName = %s,size = %d",
                file1.getName(), file1.getOriginalFilename(), file1.getSize());
        log.info(content);
        return content;
    }

    @PostMapping(value = "/file3")
    public String file3(@RequestParam(name = "file1") MultipartFile multipartFile) {
        String content = String.format("name = %s,originName = %s,size = %d",
                multipartFile.getName(), multipartFile.getOriginalFilename(), multipartFile.getSize());
        log.info(content);
        return content;
    }
    //6.请求头及注解@RequestHeader

    //7.Cookie及注解@CookieValue
    //8.Model类型参数及@ModelAttribute
    @GetMapping(value = "/model")
    public String model(Model model, ModelMap modelMap) {
        log.info("{}", model == modelMap);
        return "success";
    }
    //9.Errors或者BindingResult参数
    @PostMapping(value = "/errors")
    public String errors(@RequestBody @Validated ErrorsModel errors, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            for (ObjectError objectError : bindingResult.getAllErrors()) {
                log.warn("name={},message={}", objectError.getObjectName(), objectError.getDefaultMessage());
            }
        }
        return errors.toString();
    }

    //10.@Value
    @GetMapping(value = "/value")
    public String value(@Value(value = "${spring.application.name}") String name) {
        log.info("spring.application.name={}", name);
        return name;
    }

    /**
     * @Description //使用@RequestParam注解的Map参数
     *
     * 这种情况下的参数处理器为 RequestParamMapMethodArgumentResolver ，
     * 使用的请求方式需要指定 Content-Type 为 x-www-form-urlencoded ，不能使用 application/json 的方式
     * @param map
     * @return java.lang.String
     * Author by cqb, Date on  22:04
     **/
    @PostMapping(value = "/map")
    public String mapArgs(@RequestParam Map<String, Object> map) {
        log.info("{}", map);
        return map.toString();
    }

}
